<template>
  <!-- 轮播图 -->
  <view>
    <view class="search-box">
      <my-search @click="gotoSearch"></my-search>
    </view>
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="item in swiperList" key="item.goods_id">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类数据展示 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item,index) in newList" key="index" @click="navClickHandler(index)">
        <image lazy-load="true" :src="item.image_src" class="nav-img"></image>
      </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor-list">
      <view class="floor-item" v-for="(item,i) in floorList" key="i">
        <!-- 楼层标题 -->
        <image lazy-load="true" :src="item.floor_title.image_src" class="floor-title"></image>
        <view class="img_box">
        <!-- 左侧大图片 -->
        <navigator class="left_img_box" :url="item.product_list[0].url">
          <image lazy-load="true" :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width+'rpx'}" mode="widthFix"></image>
        </navigator>
        <!-- 右侧四个图片 -->
        <view class="right_img_box">
          <navigator class="right_img_item" v-for="(item,i) in item.product_list" v-if="i!=0" key="i" :url="item.url">
            <image lazy-load="true" :src="item.image_src" :style="{width:item.image_width+'rpx'}" mode="widthFix"></image>
      </navigator>
        </view>
      </view>
      </view>
    </view>
  </view>
  
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    mixins:[badgeMix],
    data() {
      return {
        //轮播图的数据
        swiperList:[],
        //分类导航数据
        newList:[],
        //楼层数据
        floorList:[]
      };
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getfloorList()
    },
      
    methods:{
      async getSwiperList(){
        const {data:res} = await uni.$http.get('/api/public/v1/home/swiperdata')
        //获取数据失败
        if(res.meta.status !== 200) return uni.$showMsg()
        // console.log(res);
        //获取数据成功，将数据赋值给swiperlist
        this.swiperList = res.message
      },
      async getNavList(){
        const {data:res} = await uni.$http.get('/api/public/v1/home/catitems')
        if(res.meta.status!=200) return uni.$showmsg()
        this.newList = res.message
      },
      navClickHandler(item){
        console.log(item);
        if(item==0) {
          uni.switchTab({
            url:'/pages/cate/cate'
          })
        }
      },
      async getfloorList(){
        const {data:res} = await uni.$http.get('/api/public/v1/home/floordata')
        if(res.meta.status!=200) return uni.$showmsg()
        
        //通过双层for循环处理url地址
        res.message.forEach(floor=>{
          floor.product_list.forEach(pro=>{
            pro.url = '/subpkg/goods_list/goods_list?'+pro.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
        
      },
      gotoSearch(){
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
.search-box{
  //设置定位效果为吸顶
  position: sticky;
  //吸顶的位置
  top: 0;
  //提高层级，防止被轮播图覆盖
  z-index: 999;
}
  swiper{
    height: 330rpx;
    .swiper-item ,image{
      width: 100%;
      height: 100%;
    }
  }
  .nav-list{
    display: flex;
    justify-content: space-around;
    margin: 15px 0;
    image{
      width: 128rpx;
      height: 140rpx;
    }
  }
  .floor-title{
    display: flex;
    height: 60rpx;
    width: 100%;
    
  }
  .img_box{
    display: flex;
    padding: 10rpx;
    .right_img_box{
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
    }
  }
</style>
